use crate::http::server::error::Error;
use crate::http::server::handler::result::IntoJsonResponseResult;
use crate::http::server::middleware::ValidatedJson;
use crate::{controller, dto};
use axum::Json;
use axum::extract::State;
use std::sync::Arc;

pub struct NetworkAclRuleHandler {
    controller_set: Arc<controller::ControllerSet>,
}

impl NetworkAclRuleHandler {
    pub fn new(controller_set: Arc<controller::ControllerSet>) -> Self {
        NetworkAclRuleHandler { controller_set }
    }

    pub async fn set_ingress_rules(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::network_acl_rule::SetRulesRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .network_acl_rule
            .set_ingress_rules(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn set_egress_rules(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::network_acl_rule::SetRulesRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<()>>, Error> {
        state
            .controller_set
            .network_acl_rule
            .set_egress_rules(&request.0)
            .await
            .into_json_response_result()
    }

    pub async fn all_ingress_rules(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::network_acl_rule::AllIngressRulesRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::network_acl_rule::AllIngressRulesResponseData>>, Error> {
        state
            .controller_set
            .network_acl_rule
            .all_ingress_rules(&request.0)
            .await
            .into_json_response_result()
    }
    
    pub async fn all_egress_rules(
        State(state): State<Arc<Self>>,
        request: ValidatedJson<dto::network_acl_rule::AllEgressRulesRequest>,
    ) -> Result<Json<wanmesh_common::dto::Response<dto::network_acl_rule::AllEgressRulesResponseData>>, Error> {
        state
            .controller_set
            .network_acl_rule
            .all_egress_rules(&request.0)
            .await
            .into_json_response_result()
    }
}
